package com.example.yeujian5.yuejian5.model;

import com.example.yeujian5.yuejian5.Interface.ActivityFace;
import com.example.yeujian5.yuejian5.Listenner.ActivityDetailListener;
import com.example.yeujian5.yuejian5.Listenner.ActivityListener;
import com.example.yeujian5.yuejian5.Listenner.OrderListener;
import com.example.yeujian5.yuejian5.bean.ActivityBean;
import com.example.yeujian5.yuejian5.service.ActivityService;

import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wangmengmeng on 2017/10/9.
 */

public class ActivityModel implements ActivityFace {
    private Retrofit retrofit;
    private ActivityService activityService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址

    public ActivityModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获得活动列表

     * @param listListener 监听对象
     */
    @Override
    public void getList(String id, String activity_name,final ActivityListener listListener) {
        activityService = retrofit.create(ActivityService.class);
        Call<List<ActivityBean>> call =activityService.getActivityList();
        call.enqueue(new Callback<List<ActivityBean>>() {
            @Override
            public void onResponse(Call<List<ActivityBean>> call, Response<List<ActivityBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<ActivityBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }


    /**
     * 获得活动详情
     */


    @Override
    public void getActivityById( String id,final ActivityDetailListener listener) {
        activityService = retrofit.create(ActivityService.class);
        Call<ActivityBean> call= activityService.getActivityById(id);
        call.enqueue(new Callback<ActivityBean>() {
            @Override
            public void onResponse(Call<ActivityBean> call, Response<ActivityBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<ActivityBean> call, Throwable t) {

            }
        });
    }



    //活动订单
    @Override
    public void signActivity( String acname,String activity_id,String telephone,String Anum,String Stunum,String Pay_type,
                              String Aprice,String notice,int userid,final OrderListener listener) {
        activityService = retrofit.create(ActivityService.class);
        Call<Integer> call= activityService.signActivity("signactivity",acname,activity_id,telephone,Anum,Stunum,Pay_type,Aprice,notice,"保险详细内容",userid,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }





}
